# -*- coding:utf-8 -*-
import pandas as pd
from thrift.transport import TSocket

from hbase import Hbase
from hbase.ttypes import *
# Make socket=
transport = TSocket.TSocket('10.200.2.104', 9090)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
client = Hbase.Client(protocol)
# Connect!
transport.open()

tableName = 'competition_ticket_commodity_h_lmm'
scanner=client.scannerOpen(tableName,"","")
r=client.scannerGetList(scanner,200000000)
X=[]
sale_price=[]
commodity_id=[]
insert_time=[]
is_holiday=[]
product_id=[]
is_hot=[]
comm_type=[]
for i in range(len(r)):
    dataDict= dict(map(lambda (k, v): (k, v.value), r[i].columns.items()))
    price=dataDict.get("variable:sale_price")
    if price!=None:
        commodity_id.append(int(dataDict.get("variable:commodity_id")))
        insert_time.append(int(dataDict.get("variable:insert_time")))
        product_id.append(int(dataDict.get("variable:product_id")))
        X.append([int(dataDict.get("variable:commodity_id")),int(dataDict.get("variable:insert_time"))])
        comm_type.append(dataDict.get("variable:commodity_type"))
        sale_price.append(float(dataDict.get("variable:sale_price")))
#对时间进行标记，判断是否为周末节假日等
# holidays=HolidayDate.getHoliday()
# cal = Calendar(workdays=[MO,TU,WE,TH,FR])
# for i in range(len(insert_time)):
#     a=time.strptime(str(insert_time[i]),'%Y%m%d')
#     yy,m,d = a[0:3]
#     xx2=datetime.datetime(yy,m,d)
#     splot = 0
#     if cal.isworkday(xx2):
#         splot=0
#     else:
#         splot=1
#     if str(insert_time[i]) in holidays:
#         splot=2
#     is_holiday.append(splot)

data = pd.DataFrame({'commodity_id': commodity_id, 'insert_time': insert_time, 'product_id': product_id,'comm_type':comm_type, 'sale_price': sale_price})
data.to_csv('dataHaveCommType.csv',index=False,na_rep='NaN',header=False)
transport.close()